<?php

namespace App\Http\Controllers\System;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Service\Indent\CourierService;
use App\Service\Indent\OrderService;
use App\Service\System\SystemService;
use Illuminate\Http\Request;
use DB;
use Mockery\Exception;

class SystemController extends Controller
{
    public function ideaFeedBack(Request $request)
    {
        $uid = $request->uid;
        $introduce = $request->introduce;
        $contact = $request->contact;

        $data = array(
            'uid' => $uid,
            'introduce' => $introduce,
            'contact' => $contact,
            'created_at' => date('Y-m-d H:i:s'),
        );
        if(!SystemService::addContactLog($data)){
            Result::msg('-11','error',"更新失败");
        }

        Result::msg('0','success',$data);
    }
	
	public function appVersion()
    {
        $data = DB::table('lsj_config')->where('name','now_version')->first();

        $version_array = explode(',',$data->value);
		$data = array(
			'android_version' => $version_array[0],
            'ios_version' => $version_array[1],
            'value' => $data->pop_value,
            'android_url' => $data->android_url,
            'ios_url' => $data->ios_url
		);
        Result::msg('0','success',$data);
    }

    public function QiniuUploadToken()
    {
        $token = SystemService::getUploadToken();
        $data = array(
            'token' => $token
        );
        Result::msg('0','success',$data);
    }

    /**
     * 查询物流接口
     * @param Request $request
     */
    public function courierInfo(Request $request)
    {
        $order_id = $request->oid;
        $courier_info = CourierService::getOrderCourier($order_id);

        if(!$courier_info){
            Result::msg('-57','error','暂时没有物流信息');
        }

        $courier_info = (array)$courier_info;

        try{
            $time = date('Y-m-d H:i:s')-3600;
            if(empty($courier_info['updated']) || $courier_info['state']!=3 ||(strtotime($courier_info['updated'])<strtotime($time))){
                //调用快递鸟接口查询
                $res = CourierService::birdApi($courier_info['code'],$courier_info['courier_code']);

                $courier = json_encode($res['Traces'],JSON_UNESCAPED_UNICODE);

                CourierService::changeOrderCourier($order_id,[
                    'courier_info' => $courier,
                    'updated_at' => date('Y-m-d H:i:s',time()),
                    'state' => $res['State']
                ]);
                $data = [
                    'courier_company' => $courier_info['courier_company'],
                    'courier_code' => $courier_info['courier_code'],
                    'courier_info' => $courier
                ];

            }else{
                $data = [
                    'courier_company' => $courier_info['courier_company'],
                    'courier_code' => $courier_info['courier_code'],
                    'courier_info' => $courier_info['courier_info']
                ];
            }

            Result::msg('0','success',$data);
        }catch (Exception $e){
            Result::msg('-58','error','物流信息失败');
        }
    }
}